package com.light.gamecomponents;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;

import android.graphics.Color;
import android.graphics.Typeface;

import com.badlogic.gdx.math.Vector2;
import com.huw.cameramanagers.CameraManager.FollowStatus;
import com.light.gamecomponents.physics.RollerPhysicsWorld;
import com.light.roller.Roller;

public class LevelText extends FlyingText{
	LoopEntityModifier entityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(1, 0.00001f, 1.0f),
            new AlphaModifier(3, 1.0f, 1.0f),
            new AlphaModifier(1, 1, 0.0f)));
	
	protected float[] m_positionXs = { 50,
			   60,
			   Roller.CAMERA_WIDTH - 50,
			   Roller.CAMERA_WIDTH - 60 };

	protected float[] m_positionYs = { 60,
			   Roller.CAMERA_HEIGHT - 50,
			   Roller.CAMERA_HEIGHT - 60,
			   50 };
	public LevelText(Roller game, Vector2 triggerPosition,
			Vector2 triggerRectangle, String text) {
		super(game, triggerPosition, triggerRectangle, text);
	
	}
	@Override
	public void onLoadResources() {
		this.m_fontTexture = new Texture(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.m_font = new Font(this.m_fontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.GREEN);
		m_game.getEngine().getTextureManager().loadTexture(this.m_fontTexture);
		m_game.getEngine().getFontManager().loadFont(this.m_font);
		
	}
	
	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWrold) {
		
		m_scene = scene;
		m_text = Roller.getStageName(Roller.CurrentNum);
		m_position = new Vector2(50, 60);
		m_printText = new ChangeableText(m_position.x, m_position.y, m_font,
									     m_text, 512);
		m_printText.setBlendFunction(GL10.GL_SRC_ALPHA,
									 GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_printText.setAlpha(0.000001f);
		m_printText.setVisible(false);
		m_printText.setHeight(1.5f);
		scene.attachChild(m_printText);
		m_player = Roller.player;
		m_physicsWorld = ((RollerPhysicsWorld)physicsWrold);
		m_physicsWorld.registerUpdateHandler(this);
		
	}
	
	@Override
	public void onUpdate(float pSecondsElapsed) {
		if (Roller.cameraManager.isLightOn() && 
			Roller.PreNum != Roller.CurrentNum &&
			!m_triggered)
			trigger();
		if (Roller.cameraManager.followStatus == FollowStatus.Gaming &&
			m_triggered &&
			m_printText.getAlpha() == 1)
		{
			m_printText.unregisterEntityModifier(entityModifier);
			LoopEntityModifier tempEntityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(1, 1.0f, 0.0f)));
			m_printText.registerEntityModifier(tempEntityModifier);
		}
			
		Camera tempCamera = Roller.cameraManager.getCamera();
    	setByCameraPosition(tempCamera.getCenterX(), tempCamera.getCenterY());
    	if (m_triggered)
		{
			if (m_printText.getAlpha() != 0)
			{
				
			}
			else
			{
				((RollerPhysicsWorld)((Roller)m_game).getPhysicWorld()).unregisterUpdateHandler(this);
				m_scene.detachChild(m_printText);
			}
		}
    	
	}
	
	public void trigger()
	{
		m_triggered = true;
		m_scene.detachChild(m_printText);
		m_scene.attachChild(m_printText);
		m_printText.setVisible(true);
		m_printText.registerEntityModifier(entityModifier);
	}
	


}
